/**
 iTextUploader
 (C) 2008 Asgl (myselftdev@gmail.com)
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; version 2
 of the License.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/* Revision: $Id: MainView.m 20 2008-08-21 15:44:04Z myselftdev $ */

#import "MainView.h"

@implementation MainView

@synthesize contentView;
@synthesize navBar;
@synthesize navItem;
@synthesize internetConnectionAvailable;
@synthesize tabBar;

// Initialize window and main layout
- (id)initWithFrame:(CGRect)rect parentObject:(id)theParentObject
{
	if (self = [super initWithFrame:rect])
	{
		parentObject = [theParentObject retain];
		
		CGRect contentRect = CGRectMake(rect.origin.x, 0.0, rect.size.width, (rect.size.height - 93.0));
		// Content view
		contentView = [ [UIView alloc] initWithFrame: CGRectMake(contentRect.origin.x, (contentRect.origin.y + 44.0), contentRect.size.width, contentRect.size.height)];
		contentView.contentMode = UIViewContentModeBottom;
		
		// Navigation bar
		navBar = [ [UINavigationBar alloc] initWithFrame: CGRectMake(rect.origin.x, 0.0, rect.size.width, 44.0)];
		navBar.delegate = self;
		//navBar.barStyle = UIBarStyleBlackOpaque;
		navItem = [ [UINavigationItem alloc] initWithTitle:@"iTextUploader"];
		
		[navBar pushNavigationItem:navItem animated:YES];
		
		// Tab bar
		tabBar = [ [UITabBar alloc] initWithFrame: CGRectMake(rect.origin.x, (rect.size.height - 49.0), rect.size.width, 49.0)];
		tabBar.delegate = parentObject;
		
		tabBarHomeImage = [[UIImage imageNamed:@"Home.png"] retain];
		tabBarHomeItem = [ [UITabBarItem alloc] initWithTitle:@"Home" image:tabBarHomeImage tag:1];
		
		tabBarUploadImage = [[UIImage imageNamed:@"Upload.png"] retain];
		tabBarUploadItem = [ [UITabBarItem alloc] initWithTitle:@"Upload" image:tabBarUploadImage tag:2];
		
		tabBarBrowseImage = [[UIImage imageNamed:@"Browse.png"] retain];
		tabBarBrowseItem = [ [UITabBarItem alloc] initWithTitle:@"Browse" image:tabBarBrowseImage tag:4];
		
		tabBarExportedImage = [[UIImage imageNamed:@"Exported.png"] retain];
		tabBarExportedItem = [ [UITabBarItem alloc] initWithTitle:@"Exported" image:tabBarExportedImage tag:5];
		
		tabBarPreferencesImage = [[UIImage imageNamed:@"Preferences.png"] retain];
		tabBarPreferencesItem = [ [UITabBarItem alloc] initWithTitle:@"Preferences" image:tabBarPreferencesImage tag:3];
		
		tabBarItems = [ [NSArray alloc] initWithObjects:tabBarHomeItem, tabBarUploadItem, tabBarBrowseItem, tabBarExportedItem, tabBarPreferencesItem, nil];
		
		[tabBar setItems:tabBarItems animated:YES];
		tabBar.selectedItem = tabBarHomeItem;
		
		// Add elements to window
		[self addSubview:contentView];
		[self addSubview:navBar];
		[self addSubview:tabBar];
	}
    return self;
}

- (void) hideNavBar
{
	if(!navBar.hidden)
	{
		CGRect contentFrame = [[UIScreen mainScreen] applicationFrame];
		contentFrame.size.height = contentFrame.size.height - 49.0;
		contentFrame.origin.y = 0.0;
		
		contentView.frame = contentFrame;
		navBar.hidden = YES;
	}
}

- (void) showNavBar
{
	if(navBar.hidden)
	{
		CGRect contentFrame = [[UIScreen mainScreen] applicationFrame];
		contentFrame.size.height = contentFrame.size.height - 93.0;
		contentFrame.origin.y = 44.0;
		
		contentView.frame = contentFrame;
		navBar.hidden = NO;
	}
}

- (UITabBarItem*)getTabBarItem: (NSInteger)itemId
{
	switch(itemId)
	{
			case 1:
				return [tabBarHomeItem retain];
				break;
			
			case 2:
				return [tabBarUploadItem retain];
				break;
			
			case 3:
				return [tabBarPreferencesItem retain];
				break;
			
			case 4:
				return [tabBarBrowseItem retain];
				break;
			
			case 5:
				return [tabBarExportedItem retain];
				break;
	}
	
	return nil;
}

// Deallocate resources
- (void)dealloc
{
	[parentObject release];
	
	[contentView release];
	
	[navBar release];
	[navItem release];
	
	[tabBar release];
	[tabBarItems release];
	[tabBarHomeItem release];
	[tabBarHomeImage release];
	[tabBarUploadItem release];
	[tabBarUploadImage release];
	[tabBarBrowseItem release];
	[tabBarBrowseImage release];
	[tabBarExportedItem release];
	[tabBarExportedImage release];
	[tabBarPreferencesItem release];
	[tabBarPreferencesImage release];
	
	[super dealloc];
}

@end
